package com.quectel.core.module.ai.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.ai.dao.AiControlCarDao;
import com.quectel.core.module.ai.dto.AiControlCarDto;
import com.quectel.core.module.ai.entity.AiControlCarEntity;
import com.quectel.core.module.ai.service.AiControlCarService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-11-24 19:13:09
 */
@DubboService
public class AiControlCarServiceImpl extends ServiceImpl<AiControlCarDao, AiControlCarEntity> implements AiControlCarService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AiControlCarServiceImpl.class);

    private static final String AI_CONTROL_CAR_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "AI_CONTROL_CAR_CACHE_BY_ID:";

    @Autowired
    private AiControlCarDao aiControlCarDao;


    @Override
    public void saveBatch(List<AiControlCarDto> aiControlCarDtos) {
        List<AiControlCarEntity> aiControlCarEntities = CopyUtils.copyList(aiControlCarDtos, AiControlCarEntity.class);
        for (AiControlCarEntity aiControlCarEntity : aiControlCarEntities) {
            aiControlCarEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(aiControlCarEntities);
    }

    @Override
    public AiControlCarDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                AI_CONTROL_CAR_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public AiControlCarDto selectById(Long id) {
        AiControlCarEntity entity = aiControlCarDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, AiControlCarDto.class));
    }

    @Override
    public List<AiControlCarDto> queryList(Map<String, Object> params) {
        List<AiControlCarEntity> list = aiControlCarDao.queryList(params);
        List<AiControlCarDto> result = CopyUtils.copyList(list, AiControlCarDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private AiControlCarDto paddingField(AiControlCarDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return aiControlCarDao.queryTotal(params);
    }

    @Override
    public Long save(AiControlCarDto dto) {
        AiControlCarEntity entity = CopyUtils.copyObj(dto, AiControlCarEntity.class);
        entity.setId(Snowflake.nextId());
        aiControlCarDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AiControlCarDto dto) {
        AiControlCarEntity entity = CopyUtils.copyObj(dto, AiControlCarEntity.class);
        aiControlCarDao.updateById(entity);
        CacheKit.invalidRedisCache(AI_CONTROL_CAR_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(AiControlCarDto dto) {
        AiControlCarEntity entity = CopyUtils.copyObj(dto, AiControlCarEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, aiControlCarDao);

        CacheKit.invalidRedisCache(AI_CONTROL_CAR_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        aiControlCarDao.deleteById(id);

        CacheKit.invalidRedisCache(AI_CONTROL_CAR_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
